package com.vnp.cooking.fragmenthome;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint.Join;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TextView.OnEditorActionListener;

import com.actionbarsherlock.app.SherlockListFragment;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.vnp.cooking.R;
import com.vnp.cooking.adapter.DialogItemAdapter;
import com.vnp.cooking.adapter.NhabepGridAvapter;
import com.vnp.cooking.adapter.NhabepListAdpater;
import com.vnp.cooking.bean.ObjAttribute;
import com.vnp.cooking.bean.ObjCookpad;
import com.vnp.cooking.bean.ObjCover;
import com.vnp.cooking.httpclient.HttpClientHelper;
import com.vnp.cooking.httpclient.MyJsonHttpResponseHandler;
import com.vnp.cooking.utils.KeyboardHelper;
import com.vnp.cooking.utils.PreferenceHelper;
import com.vnp.cooking.utils.Utils;

public class FragNhabep extends SherlockListFragment implements
		OnItemClickListener, OnClickListener, OnTouchListener, OnScrollListener {
	View view;
	FragNhabep frgNhabep;

	public Handler mHandler = new Handler();

	ArrayList<ObjCookpad> objCookpads = new ArrayList<ObjCookpad>();
	ObjCookpad objCookpad;

	// SLide cover
	ArrayList<ObjCover> objCovers;
	String[] urlImage;

	// search bar
	EditText edtSearch;
	ImageButton btnSort, btnSortDisplay;
	Button btnHuySearch;

	// list view va adater
	ListView listMonAn;
	NhabepListAdpater adapterList;
	LinearLayout llFocus;
	// grid view
	GridView gridView;
	NhabepGridAvapter adapterGrid;

	// Dialog
	Dialog dialog;
	View mainLayout, footerMain;

	// khai bao thanh phan bo loc
	Button btnBoloc, btnBolocLoaimonm, btnBolocLoaibua,
			btnBolocThoigian, btnBolocNguongoc, btnBolocPhuongphap,
			btnBolocDokho;
	Dialog dBoloc, dBolocSort, dBolocLm, dBolocLB, dBolocTG, dBolocNG, dBolocPP, dBolocDK;
	View clickOusite;

	// danh dau click
	boolean isClickedNL = false;
	// danh dau load more
	boolean canLoadMore = true;
	boolean isLoading = false;
	boolean isUsed = false;
	
	int mPage = 0;
	String type;
	ProgressBar progressBar, progressBar1;
	ProgressBar progressBarLoadmore;
	Button btnLoadMore;
	String typeChoose = "first";
	private int preLast;

	// type display
	int DISPLAY_LIST = 1;
	int DISPLAY_GRID = 2;

	// Search
	public String keyword = "";
	LinearLayout llSearchRs;
	ImageView btn_Clears, ic_search;
	boolean isStateSearch = false;
	TextView keyWordSearch;
	Button btnClearRs;
	int countRs;
	
	// data boloc
	PreferenceHelper preferenceHelper;
	boolean isStateFilter = false;
	
	ArrayList<ObjAttribute> listSort ;
	ArrayList<ObjAttribute> listLoaimonan = new ArrayList<ObjAttribute>();
	ArrayList<ObjAttribute> listLoaibuaan = new ArrayList<ObjAttribute>();
	ArrayList<ObjAttribute> listThoigian = new ArrayList<ObjAttribute>();
	ArrayList<ObjAttribute> listNguongoc = new ArrayList<ObjAttribute>();
	ArrayList<ObjAttribute> listPhuongphap = new ArrayList<ObjAttribute>();
	ArrayList<ObjAttribute> listDokho = new ArrayList<ObjAttribute>();
	
	ArrayList<ObjAttribute> listFilterItem = new ArrayList<ObjAttribute>();
	
	String alias;
	
	// json
	JSONObject filter = new JSONObject();
	JSONObject jo = new JSONObject();
	
	String typeSort;
	
	TextView txt_no_result;
	
	Runnable rG = new Runnable() {
		
		@Override
		public void run() {
			getDataNhabep(mPage, DISPLAY_LIST);
		}
	};
	
	
	TextView valueSort, valueLoaimon, valueLoaibuaan, valueThoigian, valueNguongoc, valuePhuongphap, valueDokho;  
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		view = (View) inflater.inflate(R.layout.frg_nha_bep, container, false);

		// mainLayout = (View) view.findViewById(R.id.main_layout);
		// KeyboardHelper.setupUI(getActivity(), view);
		preferenceHelper = new PreferenceHelper(getActivity());

		typeSort = preferenceHelper.getFilterSort();
		
		llSearchRs = (LinearLayout) view.findViewById(R.id.ll_search_rs);
		edtSearch = (EditText) view.findViewById(R.id.edt_search);
		btn_Clears = (ImageView) view.findViewById(R.id.btn_clears);
		keyWordSearch = (TextView) view.findViewById(R.id.keyword_search);
		btnClearRs = (Button) view.findViewById(R.id.btn_clear_result_search);
		ic_search = (ImageView) view.findViewById(R.id.ic_search);
		
		btnSort = (ImageButton) view.findViewById(R.id.btn_sort);
		btnSortDisplay = (ImageButton) view.findViewById(R.id.btn_sort_display);
		btnSortDisplay.setEnabled(false);
		btnHuySearch = (Button) view.findViewById(R.id.btn_huy_search);
		llFocus = (LinearLayout) view.findViewById(R.id.ll_focus);

		// khai bao bo loc
		btnBoloc = (Button) view.findViewById(R.id.btn_boloc);
		btnBolocLoaimonm = (Button) view.findViewById(R.id.btn_loaimonan);
		btnBolocLoaibua = (Button) view.findViewById(R.id.btn_loai_buaan);
		btnBolocThoigian = (Button) view.findViewById(R.id.btn_thoi_gian);
		btnBolocNguongoc = (Button) view.findViewById(R.id.btn_nguon_goc);
		btnBolocPhuongphap = (Button) view.findViewById(R.id.btn_phuong_phap);
		btnBolocDokho = (Button) view.findViewById(R.id.btn_do_kho);
		
		txt_no_result = (TextView) view.findViewById(R.id.txt_no_result);
		
		progressBar = (ProgressBar) view.findViewById(R.id.progressbar);
		gridView = (GridView) view.findViewById(R.id.gridview);
		progressBar1 = (ProgressBar) view.findViewById(R.id.progressBar1);
		if (gridView.getVisibility() == View.GONE) {
			progressBar1.setVisibility(View.GONE);
		}
		// listMonAn = (ListView) view.findViewById(R.id.list_nhabep);
		getDataNhabepHandler();
//		getDataNhabep(mPage, DISPLAY_LIST);
		adapterList = new NhabepListAdpater(getActivity(), objCookpads);
		adapterGrid = new NhabepGridAvapter(getActivity(), objCookpads);
		init();
		initFont();
		searchCookpad();
		getAttribute();
		creatSort();
		return view;
	}
	
	
	void getDataNhabepHandler() {
		mHandler.removeCallbacks(rG);
		mHandler.postDelayed(rG, 300);
	}
	void initFont() {
		btnBoloc.setTypeface(Utils.getTypefaceRegular(getActivity()));
		btnBolocLoaimonm.setTypeface(Utils.getTypefaceRegular(getActivity()));
		btnBolocLoaibua.setTypeface(Utils.getTypefaceRegular(getActivity()));
		btnBolocThoigian.setTypeface(Utils.getTypefaceRegular(getActivity()));
		btnBolocNguongoc.setTypeface(Utils.getTypefaceRegular(getActivity()));
		btnBolocPhuongphap.setTypeface(Utils.getTypefaceRegular(getActivity()));
		btnBolocDokho.setTypeface(Utils.getTypefaceRegular(getActivity()));
		edtSearch.setTypeface(Utils.getTypefaceRegular(getActivity()));
		keyWordSearch.setTypeface(Utils.getTypefaceRegular(getActivity()));
		btnClearRs.setTypeface(Utils.getTypefaceRegular(getActivity()));
		btnHuySearch.setTypeface(Utils.getTypefaceRegular(getActivity()));

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		addFooterView();
		getListView().setAdapter(adapterList);
		getListView().setOnScrollListener(this);
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		/*
		 * objCookpad = objCookpads.get(position-1); Intent t = new
		 * Intent(getActivity(), DetailCookpadActivity.class);
		 * t.putExtra("objCookpad", objCookpad); startActivity(t);
		 */
		/*
		 * objCookpad.setUrlImageCookpad(objCookpads.get(position).
		 * getUrlImageCookpad());
		 * objCookpad.setNameCookpad(objCookpads.get(position
		 * ).getNameCookpad());
		 * objCookpad.setNamePersonCookpad(objCookpads.get(position
		 * ).getNamePersonCookpad());
		 * objCookpad.setTimeCook(objCookpads.get(position).getTimeCook());
		 */

	}

	public void init() {
		edtSearch.setOnTouchListener(this);
		gridView.setOnItemClickListener(this);
		btnSortDisplay.setOnClickListener(this);
		btnSort.setOnClickListener(this);
		btnHuySearch.setOnTouchListener(this);
		// bo loc
		btnBoloc.setOnClickListener(this);
		btnBolocLoaimonm.setOnClickListener(this);
		btnBolocLoaibua.setOnClickListener(this);
		btnBolocThoigian.setOnClickListener(this);
		btnBolocNguongoc.setOnClickListener(this);
		btnBolocPhuongphap.setOnClickListener(this);
		btnBolocDokho.setOnClickListener(this);

		llFocus.setOnClickListener(this);
		llFocus.setOnTouchListener(this);
		btn_Clears.setOnClickListener(this);
		btnClearRs.setOnClickListener(this);
		
		gridView.setOnScrollListener(new OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				int lastItem  = firstVisibleItem + visibleItemCount;
				if (lastItem == totalItemCount){
					if (preLast != lastItem) {
						int last_pos = gridView.getLastVisiblePosition();
						if(last_pos>=objCookpads.size()-1&& !isLoading){
							if (gridView.getVisibility() == View.VISIBLE) {
								progressBar1.setVisibility(View.VISIBLE);
							}
							
							if (isStateSearch == true) {
								searchCT(++mPage, DISPLAY_GRID);
							} else {
								if (isStateFilter == true) {
									gotoFilter(++mPage, DISPLAY_LIST, filter, typeSort );
								} else {
									if (typeChoose.matches("first")) {
										getDataNhabep(++mPage, DISPLAY_LIST);
									} else if (typeChoose.matches(typeChoose)) {
										sortMonan(++mPage, typeChoose);
									}
								}
							}
						}
						preLast = lastItem;
					}
				}
				

					/*int last_pos = gridView.getLastVisiblePosition();
					if(last_pos>=objCookpads.size()-1 && !isLoading){
						if (isStateSearch == true) {
							searchCT(++mPage, DISPLAY_GRID);
						} else {
							if (isStateFilter == true) {
								gotoFilter(++mPage, DISPLAY_LIST, filter, typeSort );
							} else {
								if (typeChoose.matches("first")) {
									getDataNhabep(++mPage, DISPLAY_LIST);
								} else if (typeChoose.matches(typeChoose)) {
									sortMonan(++mPage, typeChoose);
								}
							}
						}
					}*/
			}
		});

	}

	public void searchCookpad() {
		edtSearch.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_SEARCH ) {
					KeyboardHelper.hideSoftKeyboard(getActivity());
					txt_no_result.setVisibility(View.GONE);
					mHandler.removeCallbacks(search);
					mHandler.postDelayed(search, 100);
					
					return true;
				}
				return false;
			}
		});

		edtSearch.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				/*new Thread(new Runnable() {

					@Override
					public void run() {
						typeChoose = "search";
						mHandler.removeCallbacks(search);
						mHandler.postDelayed(search, 2000);
					}
				}).start();*/
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				typeChoose = "search";
				keyword = s.toString();
				if (keyword.length() > 0) {
					btn_Clears.setVisibility(View.VISIBLE);
				} else {
					btn_Clears.setVisibility(View.GONE);
				}

			}
		});
	}

	Runnable search = new Runnable() {

		@Override
		public void run() {
			if (keyword == null || keyword.equals("")) {

			} else {
				if (isUsed == true) {
					isUsed = false;
					mPage = 0;
					searchCT(mPage, DISPLAY_LIST);
				} else {
					searchCT(mPage, DISPLAY_LIST);
				}
				
			}
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_sort:
			showDialogSort();
			break;

		case R.id.btn_sort_display:
			changeListGrid();
			break;

		case R.id.btn_sort_moi:
			edtSearch.setText("");
			mPage = 0;
			typeChoose = "new";
			if (txt_no_result.getVisibility() == View.VISIBLE) {
				txt_no_result.setVisibility(View.GONE);
			}
			sortMonan(mPage, "new");
			dialog.dismiss();
			break;
		case R.id.btn_sort_xem_nhieu:
			/*
			 * if (gridView.getVisibility() == View.VISIBLE) {
			 * adapterGrid.sortByViewAsc(); } else {
			 * adapterList.sortByViewAsc(); }
			 */
			edtSearch.setText("");
			mPage = 0;
			typeChoose = "view";
			if (txt_no_result.getVisibility() == View.VISIBLE) {
				txt_no_result.setVisibility(View.GONE);
			}
			sortMonan(mPage, "view");
			dialog.dismiss();
			break;
		case R.id.btn_sort_yeu_thich:
			/*
			 * if (gridView.getVisibility() == View.VISIBLE) {
			 * adapterGrid.sortByLikeAsc(); } else {
			 * adapterList.sortByLikeAsc(); }
			 */
			edtSearch.setText("");
			mPage = 0;
			typeChoose = "like";
			if (txt_no_result.getVisibility() == View.VISIBLE) {
				txt_no_result.setVisibility(View.GONE);
			}
			sortMonan(mPage, "like");
			dialog.dismiss();
			break;
		case R.id.btn_sort_danh_gia:
			/*
			 * if (gridView.getVisibility() == View.VISIBLE) { //
			 * adapterGrid.sortByRateAsc(); } else { //
			 * adapterList.sortByRateAsc(); }
			 */
			edtSearch.setText("");
			mPage = 0;
			typeChoose = "rating";
			if (txt_no_result.getVisibility() == View.VISIBLE) {
				txt_no_result.setVisibility(View.GONE);
			}
			sortMonan(mPage, "rating");
			dialog.dismiss();
			break;
		case R.id.btn_thoat:
			dialog.dismiss();
			break;

		// danh riegn cho phan bo loc
		case R.id.btn_boloc:
			showBolocChung();
			break;
		case R.id.btn_loaimonan:
			showBolocLoaimon();
			break;
		case R.id.btn_loai_buaan:
			showBolocLoaibuaan();
			break;
		case R.id.btn_thoi_gian:
			showBolocThoigian();
			break;
		case R.id.btn_nguon_goc:
			showBolocNguongoc();
			break;
		case R.id.btn_phuong_phap:
			showBolocPhuongphap();
			break;
		case R.id.btn_do_kho:
			showBolocDokho();
			break;

		case R.id.click_outside:
			dBoloc.dismiss();
			break;
		// ben trong bo loc chung
		case R.id.chung_sort:
			dBoloc.dismiss();
			showBolocSort();
			break;
		case R.id.chung_loaimon:
			dBoloc.dismiss();
			showBolocLoaimon();
			break;
		case R.id.chung_loaibuaan:
			dBoloc.dismiss();
			showBolocLoaibuaan();
			break;
		case R.id.chung_thoigian:
			dBoloc.dismiss();
			showBolocThoigian();
			break;
		case R.id.chung_nguongoc:
			dBoloc.dismiss();
			showBolocNguongoc();
			break;
		case R.id.chung_phuongphap:
			dBoloc.dismiss();
			showBolocPhuongphap();
			break;
		case R.id.chung_dokho:
			dBoloc.dismiss();
			showBolocDokho();
			break;

			
			
		// dialog nguyen lieu
		case R.id.btn_ve_boloc_chung_sort:
			dBolocSort.dismiss();
			showBolocChung();
			break;
		case R.id.click_outside_sort:
			dBolocSort.dismiss();
			break;
			
		// dialog nguyen lieu
		case R.id.btn_ve_boloc_chung:
			dBolocLm.dismiss();
			showBolocChung();
			break;
		case R.id.click_outside_nl:
			dBolocLm.dismiss();
			break;
		// loc theo loai bua na
		case R.id.btn_ve_boloc_chung_loaimon:
			dBolocLB.dismiss();
			showBolocChung();
			break;
		case R.id.btn_loc_xong_loaimon:
			dBolocLB.dismiss();
			break;
		case R.id.click_outside_loaibuaan:
			dBolocLB.dismiss();
			break;

		// bo loc thoi gian
		case R.id.btn_ve_boloc_chung_thoigian:
			dBolocTG.dismiss();
			showBolocChung();
			break;
		case R.id.btn_loc_xong_thoigian:
			dBolocTG.dismiss();
			break;
		case R.id.click_outside_thoigian:
			dBolocTG.dismiss();
			break;

		// bo loc nguon goc
		case R.id.btn_ve_boloc_chung_nguongoc:
			dBolocNG.dismiss();
			showBolocChung();
			break;
		case R.id.btn_loc_xong_nguongoc:
			dBolocNG.dismiss();
			break;
		case R.id.click_outside_nguongoc:
			dBolocNG.dismiss();
			break;

		// boloc phuong phap
		case R.id.btn_ve_boloc_chung_phuongphap:
			dBolocPP.dismiss();
			showBolocChung();
			break;
		case R.id.btn_loc_xong_phuongphap:
			dBolocPP.dismiss();
			break;
		case R.id.click_outside_phuongphap:
			dBolocPP.dismiss();
			break;

		// boloc do kho
		case R.id.btn_ve_boloc_chung_dokho:
			dBolocDK.dismiss();
			showBolocChung();
			break;
		case R.id.btn_loc_xong_dokho:
			dBolocDK.dismiss();
			break;
		case R.id.click_outside_dokho:
			dBolocDK.dismiss();
			break;
		case R.id.btn_load_more:
			mPage = mPage + 1;
			progressBarLoadmore.setVisibility(View.VISIBLE);
			btnLoadMore.setText("Đang lấy thêm công thức...");

			if (isStateSearch == true) {
				searchCT(mPage, DISPLAY_GRID);
			} else {
				if (isStateFilter == true) {
					gotoFilter(mPage, DISPLAY_LIST, filter, typeSort );
				} else {
					if (typeChoose.matches("first")) {
						getDataNhabep(mPage, DISPLAY_LIST);
					} else if (typeChoose.matches(typeChoose)) {
						sortMonan(mPage, typeChoose);
					}
				}
			}
			break;
		case R.id.btn_clears:
			edtSearch.setText("");
			btn_Clears.setVisibility(View.GONE);
			break;

		case R.id.btn_clear_result_search:
			mPage = 0;
			objCookpads.clear();
			llSearchRs.setVisibility(View.GONE);
			edtSearch.setText("");
			getDataNhabep(mPage, DISPLAY_LIST);
			break;
		}

	}
	
	

	@Override
	public void onResume() {
		super.onResume();
		setDefaultBacgroundButtonBoloc();
	}

	void setDefaultBacgroundButtonBoloc() {
		if (preferenceHelper.getFilterLoaimon().matches("")) {
			btnBolocLoaimonm.setBackgroundResource(R.drawable.button_text_selector);
		} else {
			btnBolocLoaimonm.setBackgroundColor(Color.WHITE);
		}
		
		if (preferenceHelper.getFilterLoaibuaan().matches("")) {
			btnBolocLoaibua.setBackgroundResource(R.drawable.button_text_selector);
		} else {
			btnBolocLoaibua.setBackgroundColor(Color.WHITE);
		}
		
		if (preferenceHelper.getFilterThoigian().matches("")) {
			btnBolocThoigian.setBackgroundResource(R.drawable.button_text_selector);
		} else {
			btnBolocThoigian.setBackgroundColor(Color.WHITE);
		}
		
		if (preferenceHelper.getFilterNguongoc().matches("")) {
			btnBolocNguongoc.setBackgroundResource(R.drawable.button_text_selector);
		} else {
			btnBolocNguongoc.setBackgroundColor(Color.WHITE);
		}
		
		if (preferenceHelper.getFilterPhuongphap().matches("")) {
			btnBolocPhuongphap.setBackgroundResource(R.drawable.button_text_selector);
		} else {
			btnBolocPhuongphap.setBackgroundColor(Color.WHITE);
		}
		
		if (preferenceHelper.getFilterDokho().matches("")) {
			btnBolocDokho.setBackgroundResource(R.drawable.button_text_selector);
		} else {
			btnBolocDokho.setBackgroundColor(Color.WHITE);
		}
		
		if (preferenceHelper.getFilterLoaimon().matches("")&&preferenceHelper.getFilterLoaibuaan().matches("")&&preferenceHelper.getFilterNameThoigian().matches("")
				&&preferenceHelper.getFilterNameNguongoc().matches("")&&preferenceHelper.getFilterNamePhuongphap().matches("")&&preferenceHelper.getFilterNameDokho().matches("")) {
			btnBoloc.setBackgroundResource(R.drawable.button_text_selector);
		} else {
			btnBoloc.setBackgroundColor(Color.WHITE);
		}
		
	}
	void hiddenContent() {
		Runnable openKeyboard = new Runnable() {

			@Override
			public void run() {

			}
		};
		mHandler.postDelayed(openKeyboard, 100);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		Log.i("Test", "Trang thai: " + event.getAction());
		switch (v.getId()) {
		case R.id.edt_search:
			edtSearch.setCursorVisible(true);
			btnHuySearch.setVisibility(View.VISIBLE);
			btnSort.setVisibility(View.GONE);
			btnSortDisplay.setVisibility(View.GONE);
			ic_search.setVisibility(View.GONE);
			llFocus.setVisibility(View.VISIBLE);

			break;
		case R.id.btn_huy_search:
			edtSearch.setText("");
			btnHuySearch.setVisibility(View.GONE);
			btnSort.setVisibility(View.VISIBLE);
			btnSortDisplay.setVisibility(View.VISIBLE);
			llFocus.setVisibility(View.GONE);
			ic_search.setVisibility(View.VISIBLE);
			KeyboardHelper.hideSoftKeyboard(getActivity());

			break;
		case R.id.ll_focus:
			llFocus.setVisibility(View.GONE);
			btnHuySearch.setVisibility(View.GONE);
			btnSort.setVisibility(View.VISIBLE);
			btnSortDisplay.setVisibility(View.VISIBLE);
			KeyboardHelper.hideSoftKeyboard(getActivity());
			llFocus.setFocusableInTouchMode(true);
			ic_search.setVisibility(View.VISIBLE);
			break;

		}

		return false;
	}

	// fragment
	public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
		private FragmentManager mFragmentManager;
		private Map<Integer, String> mFragmentTags;
		SparseArray<CoverFragmentSlide> map = new SparseArray<CoverFragmentSlide>();

		public MyFragmentPagerAdapter(FragmentManager fm) {
			super(fm);
			mFragmentManager = fm;
			mFragmentTags = new HashMap<Integer, String>();
		}

		@Override
		public Fragment getItem(int index) {
			CoverFragmentSlide fragment = new CoverFragmentSlide();

			Bundle b = new Bundle();
			// b.putString("urlImage",
			// createDataImageCover().get(index).getUrlImageCookpad());
			map.put(index, fragment);
			fragment.setArguments(b);

			return fragment;
		}

		@Override
		public int getCount() {
			return 4;
		}

		public CoverFragmentSlide getFragment(int pos) {
			return map.get(pos);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			Object obj = super.instantiateItem(container, position);
			if (obj instanceof Fragment) {
				// record the fragment tag here.
				Fragment f = (Fragment) obj;
				String tag = f.getTag();
				mFragmentTags.put(position, tag);
			}
			return obj;
		}

		@Override
		public int getItemPosition(Object object) {
			return POSITION_NONE;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
	}

	// show dialog sapxep
	public void showDialogSort() {
		dialog = new Dialog(getActivity(), R.style.DialogSlideAnim);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_choose_sort_type);

		// set cac phan tu ben trong
		Button btnSortMoi = (Button) dialog.findViewById(R.id.btn_sort_moi);
		Button btnSortXemnhieu = (Button) dialog
				.findViewById(R.id.btn_sort_xem_nhieu);
		Button btnSortYeuthich = (Button) dialog
				.findViewById(R.id.btn_sort_yeu_thich);
		Button btnSortDanhgia = (Button) dialog
				.findViewById(R.id.btn_sort_danh_gia);
		Button btnThoat = (Button) dialog.findViewById(R.id.btn_thoat);
		
		btnSortMoi.setTypeface(Utils.getTypefaceRegular(getActivity()));
		btnSortXemnhieu.setTypeface(Utils.getTypefaceRegular(getActivity()));
		btnSortDanhgia.setTypeface(Utils.getTypefaceRegular(getActivity()));
		btnSortYeuthich.setTypeface(Utils.getTypefaceRegular(getActivity()));
		btnThoat.setTypeface(Utils.getTypefaceRegular(getActivity()), Typeface.BOLD);

		WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
		lp.copyFrom(dialog.getWindow().getAttributes());
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;
		lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

		dialog.setCanceledOnTouchOutside(true);
		dialog.show();
		dialog.getWindow().setAttributes(lp);
		dialog.getWindow().setGravity(Gravity.BOTTOM);
		dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));

		
		
		// xu li onclick cac nut
		btnThoat.setOnClickListener(this);
		btnSortMoi.setOnClickListener(this);
		btnSortXemnhieu.setOnClickListener(this);
		btnSortYeuthich.setOnClickListener(this);
		btnSortDanhgia.setOnClickListener(this);

	}

	// change list and grid
	public void changeListGrid() {
		if (gridView.getVisibility() == View.VISIBLE) {
			gridView.setVisibility(View.GONE);
			getListView().setVisibility(View.VISIBLE);
			getListView().setAdapter(adapterList);
			btnSortDisplay.setImageResource(R.drawable.ic_grid_sort_03);
		} else {
			getListView().setVisibility(View.GONE);
			gridView.setVisibility(View.VISIBLE);
			adapterGrid = new NhabepGridAvapter(getActivity(), objCookpads);
			gridView.setAdapter(adapterGrid);
			btnSortDisplay.setImageResource(R.drawable.ic_list_sort_05);
		}
	}

	public void getDataNhabep(final int page, final int typeDisplay) {
		isLoading = true;
		isStateSearch = false;
		isStateFilter = false;
		isUsed = true;
		HttpClientHelper httpClientHelper = new HttpClientHelper(
				new MyJsonHttpResponseHandler() {

					@Override
					public void onFailure(Throwable error) {
						progressBarLoadmore.setVisibility(View.GONE);
						btnLoadMore.setText("Lấy thêm công thức");
						isLoading = false;
						btnSortDisplay.setEnabled(true);
						progressBar1.setVisibility(View.GONE);
						error.printStackTrace();
						hiddenProgessDialog();
					}

					@Override
					public void onSuccess(JSONObject response) {
						try {
							hiddenProgessDialog();
							btnSortDisplay.setEnabled(true);
							int status = response.getInt("status");

							if (status == 200) {
								if(mPage == 0) {
									adapterList.clear();
									adapterGrid.clear();
								}
								progressBarLoadmore.setVisibility(View.GONE);
								btnLoadMore.setText("Lấy thêm công thức");
								progressBar1.setVisibility(View.GONE);
								JSONArray data = response.getJSONArray("data");
								Log.i("DATA",
										"DATA nhan ve : " + data.toString());
								// check xem co load more ko
								if (data.length() < 10)
									canLoadMore = false;

								for (int i = 0; i < data.length(); i++) {
									JSONObject monan = data.getJSONObject(i);
									JSONObject tacgia = monan
											.getJSONObject("author");

									ObjCookpad objCookpad = new ObjCookpad();
									objCookpad.setNameCookpad(monan
											.getString("name"));
									// getImage
									JSONObject image = monan.getJSONObject("image");
									objCookpad.setUrlImageCookpad(image.getString("original"));
									
									objCookpad.setViewedCookpad(monan
											.getInt("view_count"));
									objCookpad.setLikeCookpad(monan
											.getInt("like_count"));

									objCookpad.setId(monan.getString("id"));
									objCookpad.setThoigianNauan(monan
											.getString("time"));
									objCookpad.setDescriptionCookpad(monan
											.getString("desc"));
									if (tacgia.isNull("name") == true) {
										objCookpad
												.setNamePersonCookpad("Vô danh");
										objCookpad.setIdNamePerson("");
									} else {
										objCookpad.setNamePersonCookpad(tacgia
												.getString("name"));
										objCookpad.setIdNamePerson(tacgia.getString("id"));
									}
									objCookpads.add(objCookpad);
								}
								isLoading = false;
								if (typeDisplay == DISPLAY_LIST) {
//									adapterList.notifyDataSetChanged();
								} else {
//									adapterGrid.notifyDataSetChanged();
								}
								footerMain.setVisibility(View.VISIBLE);

							} else {
								progressBarLoadmore.setVisibility(View.GONE);
								btnLoadMore.setText("Lấy thêm công thức");
								progressBar1.setVisibility(View.GONE);
								/*Toast.makeText(getActivity(),
										"Không lấy được danh sách công thức",
										Toast.LENGTH_SHORT).show();*/
							}

						} catch (Exception e) {
							e.printStackTrace();
							progressBarLoadmore.setVisibility(View.GONE);
							btnLoadMore.setText("Lấy thêm công thức");
							progressBar1.setVisibility(View.GONE);
						}
					}
				});

		httpClientHelper.getDataNhabep(page);
		if (page==0) {
			getListView().setSelection(0);
		}
	}

	// sort mon an
	public void sortMonan(final int page, String type) {
		isLoading = true;
		isStateSearch = false;
		isStateFilter = false;
		isUsed = true;
		HttpClientHelper httpClientHelper = new HttpClientHelper(
				new MyJsonHttpResponseHandler() {

					@Override
					public void onFailure(Throwable error) {
						progressBarLoadmore.setVisibility(View.GONE);
						btnLoadMore.setText("Lấy thêm công thức");
						isLoading = false;
						progressBar1.setVisibility(View.GONE);
						error.printStackTrace();
						hiddenProgessDialog();
					}

					@Override
					public void onSuccess(JSONObject response) {
						try {
							hiddenProgessDialog();
							int status = response.getInt("status");

							if (status == 200) {
								progressBarLoadmore.setVisibility(View.GONE);
								btnLoadMore.setText("Lấy thêm công thức");
								progressBar1.setVisibility(View.GONE);
								JSONArray data = response.getJSONArray("data");
								Log.i("DATA", "DATA cua " + data.toString());
								// check xem co load more ko
								if (data.length() < 10)
									canLoadMore = false;
								if (page > 0) {

								} else {
									adapterList.clear();
								}
								for (int i = 0; i < data.length(); i++) {
									JSONObject monan = data.getJSONObject(i);
									JSONObject tacgia = monan
											.getJSONObject("author");

									ObjCookpad objCookpad = new ObjCookpad();
									objCookpad.setNameCookpad(monan
											.getString("name"));
									// getImage
									JSONObject image = monan.getJSONObject("image");
									objCookpad.setUrlImageCookpad(image.getString("cover"));
									
									objCookpad.setViewedCookpad(monan
											.getInt("view_count"));
									objCookpad.setLikeCookpad(monan
											.getInt("like_count"));

									objCookpad.setId(monan.getString("id"));
									objCookpad.setThoigianNauan(monan
											.getString("time"));
									objCookpad.setDescriptionCookpad(monan
											.getString("desc"));
									if (tacgia.isNull("name") == true) {
										objCookpad
												.setNamePersonCookpad("Vô danh");
										objCookpad.setIdNamePerson("");
									} else {
										objCookpad.setNamePersonCookpad(tacgia
												.getString("name"));
										objCookpad.setIdNamePerson(tacgia.getString("id"));
									}
									if (page > 0) {
										objCookpads.add(objCookpad);
										adapterList.notifyDataSetChanged();
									} else {
										adapterList.add(objCookpad);
										adapterList.notifyDataSetChanged();
									}
									// objCookpads.add(objCookpad);

								}
								isLoading = false;
//								adapterList.notifyDataSetChanged();
								adapterGrid.notifyDataSetChanged();
								listMonAn.setSelection(0);
							} else {
								progressBarLoadmore.setVisibility(View.GONE);
								btnLoadMore.setText("Lấy thêm công thức");
								progressBar1.setVisibility(View.GONE);
							}

						} catch (Exception e) {
							progressBarLoadmore.setVisibility(View.GONE);
							btnLoadMore.setText("Lấy thêm công thức");
							progressBar1.setVisibility(View.GONE);
							e.printStackTrace();
						}

					}

				});
		httpClientHelper.sortMonAn(page, type);
		if (page==0) {
			getListView().setSelection(0);
		}
	}

	// show dialog bo loc
	public void showBolocChung() {
		dBoloc = new Dialog(getActivity(), R.style.DialogSlideAnimLeft);
		dBoloc.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dBoloc.setContentView(R.layout.dilog_boloc_chung);

		// khai bao cac thanh phan
		View clickOusite = (View) dBoloc.findViewById(R.id.click_outside);
		Button btnXong = (Button) dBoloc.findViewById(R.id.btn_loc_xong);
		Button btnRefresh = (Button) dBoloc
				.findViewById(R.id.btn_refresh_boloc);

		// cac thanh phan loc
		
		View chungSort = (View) dBoloc.findViewById(R.id.chung_sort);
		
		View chungLM = (View) dBoloc.findViewById(R.id.chung_loaimon);
		View chungLB = (View) dBoloc.findViewById(R.id.chung_loaibuaan);
		View chungTG = (View) dBoloc.findViewById(R.id.chung_thoigian);
		View chungNG = (View) dBoloc.findViewById(R.id.chung_nguongoc);
		View chungPP = (View) dBoloc.findViewById(R.id.chung_phuongphap);
		View chungDK = (View) dBoloc.findViewById(R.id.chung_dokho);
		
		// title
		TextView titleSapxep = (TextView) dBoloc.findViewById(R.id.title_sapxep);
		TextView titleLoaimon = (TextView) dBoloc.findViewById(R.id.title_loaimon);
		TextView titleLoaibuaan = (TextView) dBoloc.findViewById(R.id.title_loaibuaan);
		TextView titleThoigian = (TextView) dBoloc.findViewById(R.id.title_thoigian);
		TextView titleNguongoc = (TextView) dBoloc.findViewById(R.id.title_nguongoc);
		TextView titlePhuongphap = (TextView) dBoloc.findViewById(R.id.title_phuongphap);
		TextView titleDokho = (TextView) dBoloc.findViewById(R.id.title_dokho);
		
		// value
		valueSort = (TextView) dBoloc.findViewById(R.id.value_sapxep);
		valueLoaimon = (TextView) dBoloc.findViewById(R.id.value_loaimon);
		valueLoaibuaan = (TextView) dBoloc.findViewById(R.id.value_loaibuaan);
		valueThoigian = (TextView) dBoloc.findViewById(R.id.value_thoigian);
		valueNguongoc = (TextView) dBoloc.findViewById(R.id.value_nguongoc);
		valuePhuongphap = (TextView) dBoloc.findViewById(R.id.value_phuongphap);
		valueDokho = (TextView) dBoloc.findViewById(R.id.value_dokho);
		
		
		// setfont
		titleSapxep.setTypeface(Utils.getTypefaceRegular(getActivity()));
		titleLoaimon.setTypeface(Utils.getTypefaceRegular(getActivity()));
		titleLoaibuaan.setTypeface(Utils.getTypefaceRegular(getActivity()));
		titleThoigian.setTypeface(Utils.getTypefaceRegular(getActivity()));
		titleNguongoc.setTypeface(Utils.getTypefaceRegular(getActivity()));
		titlePhuongphap.setTypeface(Utils.getTypefaceRegular(getActivity()));
		titleDokho.setTypeface(Utils.getTypefaceRegular(getActivity()));
		
		
		
		btnXong.setTypeface(Utils.getTypefaceRegular(getActivity()));
		btnRefresh.setTypeface(Utils.getTypefaceRegular(getActivity()));
		valueSort.setTypeface(Utils.getTypefaceRegular(getActivity()));
		valueLoaimon.setTypeface(Utils.getTypefaceRegular(getActivity()));
		valueLoaibuaan.setTypeface(Utils.getTypefaceRegular(getActivity()));
		valueNguongoc.setTypeface(Utils.getTypefaceRegular(getActivity()));
		valuePhuongphap.setTypeface(Utils.getTypefaceRegular(getActivity()));
		valueDokho.setTypeface(Utils.getTypefaceRegular(getActivity()));
		valueThoigian.setTypeface(Utils.getTypefaceRegular(getActivity()));
		
		// set value
		
		if (preferenceHelper.getFilterSort().matches("")) {
			valueSort.setText("Tất cả");	
			valueSort.setTextColor(getResources().getColor(R.color.color_text_default));
			
		} else {
			valueSort.setText(preferenceHelper.getFilterNameSort());		
			valueSort.setTextColor(getResources().getColor(R.color.color_red));
		}
		Log.i("DATA", "Loai mon: "+preferenceHelper.getFilterNameLoaimon());
		if(preferenceHelper.getFilterLoaimon().matches("")) {
			valueLoaimon.setText("Tất cả");	
			valueLoaimon.setTextColor(getResources().getColor(R.color.color_text_default));
		} else {
			valueLoaimon.setText(preferenceHelper.getFilterNameLoaimon());
			valueLoaimon.setTextColor(getResources().getColor(R.color.color_red));
		}
			
		
		if (preferenceHelper.getFilterLoaibuaan().matches("")) {
			valueLoaibuaan.setText("Tất cả");	
			valueLoaibuaan.setTextColor(getResources().getColor(R.color.color_text_default));
		} else {
			valueLoaibuaan.setText(preferenceHelper.getFilterNameLoaibuaan());		
			valueLoaibuaan.setTextColor(getResources().getColor(R.color.color_red));
		}
				
		if (preferenceHelper.getFilterThoigian().matches("")) {
			valueThoigian.setText("Tất cả");	
			valueThoigian.setTextColor(getResources().getColor(R.color.color_text_default));
		} else {
			valueThoigian.setText(preferenceHelper.getFilterNameThoigian());
			valueThoigian.setTextColor(getResources().getColor(R.color.color_red));
		}
		
		if (preferenceHelper.getFilterNguongoc().matches("")) {
			valueNguongoc.setText("Tất cả");
			valueNguongoc.setTextColor(getResources().getColor(R.color.color_text_default));
		} else {
			valueNguongoc.setText(preferenceHelper.getFilterNameNguongoc());
			valueNguongoc.setTextColor(getResources().getColor(R.color.color_red));
		}
		
		if (preferenceHelper.getFilterPhuongphap().matches("")) {
			valuePhuongphap.setText("Tất cả");
			valuePhuongphap.setTextColor(getResources().getColor(R.color.color_text_default));
		} else {
			valuePhuongphap.setText(preferenceHelper.getFilterNamePhuongphap());
			valuePhuongphap.setTextColor(getResources().getColor(R.color.color_red));
		}
		
		if (preferenceHelper.getFilterDokho().matches("")) {
			valueDokho.setText("Tất cả");
			valueDokho.setTextColor(getResources().getColor(R.color.color_text_default));
		} else {
			valueDokho.setText(preferenceHelper.getFilterNameDokho());
			valueDokho.setTextColor(getResources().getColor(R.color.color_red));
		}
		
		
		
		WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
		lp.copyFrom(dBoloc.getWindow().getAttributes());
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;
		lp.height = WindowManager.LayoutParams.MATCH_PARENT;

		dBoloc.setCanceledOnTouchOutside(true);
		dBoloc.show();
		dBoloc.getWindow().setBackgroundDrawable(new ColorDrawable(0));
		dBoloc.getWindow().setAttributes(lp);
		dBoloc.getWindow().setGravity(Gravity.RIGHT);

		// set su kien click cho cac thanh phan
		clickOusite.setOnClickListener(this);
		btnXong.setOnClickListener(this);
		btnRefresh.setOnClickListener(this);

		// click cac thanh phan ben trong boloc
		chungSort.setOnClickListener(this);
		chungLM.setOnClickListener(this);
		chungLB.setOnClickListener(this);
		chungTG.setOnClickListener(this);
		chungNG.setOnClickListener(this);
		chungPP.setOnClickListener(this);
		chungDK.setOnClickListener(this);
		
		btnXong.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				createFilter(preferenceHelper.getFilterLoaimon(), preferenceHelper.getFilterLoaibuaan(), preferenceHelper.getFilterThoigian(), preferenceHelper.getFilterNguongoc(), preferenceHelper.getFilterPhuongphap(), preferenceHelper.getFilterDokho());
				gotoFilter(mPage, DISPLAY_LIST, filter, typeSort);
				dBoloc.dismiss();
			}
		});
		
		btnRefresh.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				preferenceHelper.setFilterLoaimonan("");
				preferenceHelper.setFilterLoaibuaan("");
				preferenceHelper.setFilterThoigian("");
				preferenceHelper.setFilterNguocgoc("");
				preferenceHelper.setFilterPhuongphap("");
				preferenceHelper.setFilterDokho("");
				preferenceHelper.setFilterSort("");
				
				preferenceHelper.setFilterNameLoaimonan("");
				preferenceHelper.setFilterNameLoaibuaan("");
				preferenceHelper.setFilterNameThoigian("");
				preferenceHelper.setFilterNameNguocgoc("");
				preferenceHelper.setFilterNamePhuongphap("");
				preferenceHelper.setFilterNameDokho("");
				preferenceHelper.setFilterNameSort("");
				
				preferenceHelper.setFilterChecked("");
				
				valueLoaibuaan.setText("Tất cả");
				valueLoaimon.setText("Tất cả");
				valueThoigian.setText("Tất cả");
				valueNguongoc.setText("Tất cả");
				valuePhuongphap.setText("Tất cả");
				valueDokho.setText("Tất cả");
				valueSort.setText("Tất cả");
				
				// set ve color mac dinh
				valueThoigian.setTextColor(getResources().getColor(R.color.color_text_default));
				valueLoaibuaan.setTextColor(getResources().getColor(R.color.color_text_default));
				valueLoaimon.setTextColor(getResources().getColor(R.color.color_text_default));
				valueNguongoc.setTextColor(getResources().getColor(R.color.color_text_default));
				valuePhuongphap.setTextColor(getResources().getColor(R.color.color_text_default));
				valueDokho.setTextColor(getResources().getColor(R.color.color_text_default));
				valueSort.setTextColor(getResources().getColor(R.color.color_text_default));
				
				btnBolocLoaimonm.setBackgroundResource(R.drawable.button_text_selector);
				btnBolocLoaibua.setBackgroundResource(R.drawable.button_text_selector);
				btnBolocThoigian.setBackgroundResource(R.drawable.button_text_selector);
				btnBolocNguongoc.setBackgroundResource(R.drawable.button_text_selector);
				btnBolocPhuongphap.setBackgroundResource(R.drawable.button_text_selector);
				btnBolocDokho.setBackgroundResource(R.drawable.button_text_selector);
				btnBoloc.setBackgroundResource(R.drawable.button_text_selector);
			}
		});
		
	}
	
	
	
	// show dialog sort
		public void showBolocSort() {
			dBolocSort = new Dialog(getActivity(), R.style.DialogSlideAnimLeft);
			dBolocSort.requestWindowFeature(Window.FEATURE_NO_TITLE);
			dBolocSort.setContentView(R.layout.dilog_boloc_sort);

			// khai bao cac thanh phan
			View clickOusite = (View) dBolocSort.findViewById(R.id.click_outside_sort);
			Button btnXong = (Button) dBolocSort.findViewById(R.id.btn_loc_xong_sort);
			Button btnBack = (Button) dBolocSort
					.findViewById(R.id.btn_ve_boloc_chung_sort);

			// add list nguyen lieu vÃ  dialgog
			ListView listvSort = (ListView) dBolocSort
					.findViewById(R.id.list_sort);
			final DialogItemAdapter adapterSort = new DialogItemAdapter(getActivity(),
					getValueDialog(0));
			listvSort.setAdapter(adapterSort);

			WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
			lp.copyFrom(dBolocSort.getWindow().getAttributes());
			lp.width = WindowManager.LayoutParams.MATCH_PARENT;
			lp.height = WindowManager.LayoutParams.MATCH_PARENT;

			dBolocSort.setCanceledOnTouchOutside(true);
			dBolocSort.show();
			dBolocSort.getWindow().setBackgroundDrawable(new ColorDrawable(0));
			dBolocSort.getWindow().setAttributes(lp);
			dBolocSort.getWindow().setGravity(Gravity.RIGHT);

			// set su kien click cho cac thanh phan
			clickOusite.setOnClickListener(this);
			btnXong.setOnClickListener(this);
			btnBack.setOnClickListener(this);
			// item con ben trog
			listvSort.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int position,
						long arg3) {
					Log.i("DATA", "VALUE: name :"+getValueDialog(0).get(position).getName());
					preferenceHelper.setFilterSort(getValueDialog(0).get(position).getAlias());
					preferenceHelper.setFilterNameSort(getValueDialog(0).get(position).getName());
					preferenceHelper.setFilterChecked(getValueDialog(0).get(position).getAlias());
					adapterSort.notifyDataSetChanged();
					
					typeSort = preferenceHelper.getFilterSort();
				}
				
			});
			btnXong.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Log.i("DATA", "JSON: "+filter);
					gotoFilter(mPage, DISPLAY_LIST, filter, typeSort);
					dBolocSort.dismiss();
				}
			});
			
			
		}

	// show dialog bo loc
	public void showBolocLoaimon() {
		dBolocLm = new Dialog(getActivity(), R.style.DialogSlideAnimLeft);
		dBolocLm.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dBolocLm.setContentView(R.layout.dilog_boloc_nguyenlieu1);

		// khai bao cac thanh phan
		View clickOusite = (View) dBolocLm.findViewById(R.id.click_outside_nl);
		Button btnXong = (Button) dBolocLm.findViewById(R.id.btn_loc_xong_nl);
		Button btnBack = (Button) dBolocLm
				.findViewById(R.id.btn_ve_boloc_chung);

		// add list nguyen lieu vÃ  dialgog
		ListView listNL = (ListView) dBolocLm
				.findViewById(R.id.list_nguyenlieu);
		final DialogItemAdapter adapterNl = new DialogItemAdapter(getActivity(),
				getValueDialog(1));
		listNL.setAdapter(adapterNl);

		WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
		lp.copyFrom(dBolocLm.getWindow().getAttributes());
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;
		lp.height = WindowManager.LayoutParams.MATCH_PARENT;

		dBolocLm.setCanceledOnTouchOutside(true);
		dBolocLm.show();
		dBolocLm.getWindow().setBackgroundDrawable(new ColorDrawable(0));
		dBolocLm.getWindow().setAttributes(lp);
		dBolocLm.getWindow().setGravity(Gravity.RIGHT);

		// set su kien click cho cac thanh phan
		clickOusite.setOnClickListener(this);
		btnXong.setOnClickListener(this);
		btnBack.setOnClickListener(this);
		// item con ben trog
		listNL.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				Log.i("DATA", "VALUE: alias :"+getValueDialog(1).get(position).getAlias());
				Log.i("DATA", "VALUE: name :"+getValueDialog(1).get(position).getName());
				preferenceHelper.setFilterLoaimonan(getValueDialog(1).get(position).getAlias());
				preferenceHelper.setFilterNameLoaimonan(getValueDialog(1).get(position).getName());
				preferenceHelper.setFilterChecked(getValueDialog(1).get(position).getAlias());
				adapterNl.notifyDataSetChanged();
				createFilter(getValueDialog(1).get(position).getAlias(), "", "", "", "", "");
			}
			
		});
		btnXong.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.i("DATA", "JSON: "+filter);
				preferenceHelper.setFilterLoaibuaan("");
				preferenceHelper.setFilterThoigian("");
				preferenceHelper.setFilterNguocgoc("");
				preferenceHelper.setFilterPhuongphap("");
				preferenceHelper.setFilterDokho("");
				preferenceHelper.setFilterSort("");
				
				valueLoaibuaan.setText("Tất cả");
				valueThoigian.setText("Tất cả");
				valueNguongoc.setText("Tất cả");
				valuePhuongphap.setText("Tất cả");
				valueDokho.setText("Tất cả");
				valueSort.setText("Tất cả");
				
				// set ve color mac dinh
				valueThoigian.setTextColor(getResources().getColor(R.color.color_text_default));
				valueLoaibuaan.setTextColor(getResources().getColor(R.color.color_text_default));
				valueNguongoc.setTextColor(getResources().getColor(R.color.color_text_default));
				valuePhuongphap.setTextColor(getResources().getColor(R.color.color_text_default));
				valueDokho.setTextColor(getResources().getColor(R.color.color_text_default));
				valueSort.setTextColor(getResources().getColor(R.color.color_text_default));
				
				
				
				
				gotoFilter(mPage, DISPLAY_LIST, filter, typeSort);
				dBolocLm.dismiss();
			}
		});
		
		
	}
	
	 public JSONObject createFilter(String aliasLoaMonan, String aliasLoaibuaan, String aliasThoigian, String aliasNguongoc, String aliasPhuongphap, String aliasDokho) {
		try {
			filter.put("loai-mon-an", aliasLoaMonan);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			filter.put("loai-bua-an", aliasLoaibuaan);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			filter.put("thoi-gian", aliasThoigian);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			filter.put("nguon-goc", aliasNguongoc);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			filter.put("phuong-phap", aliasPhuongphap);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			filter.put("do-kho", aliasDokho);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return filter;
		}

	// show dialog bo loc loai bua an
	public void showBolocLoaibuaan() {
		dBolocLB = new Dialog(getActivity(), R.style.DialogSlideAnimLeft);
		dBolocLB.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dBolocLB.setContentView(R.layout.dilog_boloc_loaibuan);

		// khai bao cac thanh phan
		View clickOusite = (View) dBolocLB
				.findViewById(R.id.click_outside_loaibuaan);
		Button btnXong = (Button) dBolocLB
				.findViewById(R.id.btn_loc_xong_loaimon);
		Button btnBack = (Button) dBolocLB
				.findViewById(R.id.btn_ve_boloc_chung_loaimon);

		ListView listLoaibuaan = (ListView) dBolocLB
				.findViewById(R.id.list_loaimonan);
		final DialogItemAdapter adapterLoaibuaan = new DialogItemAdapter(
				getActivity(), getValueDialog(2));
		listLoaibuaan.setAdapter(adapterLoaibuaan);

		WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
		lp.copyFrom(dBolocLB.getWindow().getAttributes());
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;
		lp.height = WindowManager.LayoutParams.MATCH_PARENT;

		dBolocLB.setCanceledOnTouchOutside(true);
		dBolocLB.show();
		dBolocLB.getWindow().setBackgroundDrawable(new ColorDrawable(0));
		dBolocLB.getWindow().setAttributes(lp);
		dBolocLB.getWindow().setGravity(Gravity.RIGHT);

		// set su kien click cho cac thanh phan
		clickOusite.setOnClickListener(this);
		btnXong.setOnClickListener(this);
		btnBack.setOnClickListener(this);
		
		listLoaibuaan.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				Log.i("DATA", "VALUE: alias :"+getValueDialog(2).get(position).getAlias());
				Log.i("DATA", "VALUE: name :"+getValueDialog(2).get(position).getName());
				preferenceHelper.setFilterLoaibuaan(getValueDialog(2).get(position).getAlias());
				preferenceHelper.setFilterNameLoaibuaan(getValueDialog(2).get(position).getName());
				preferenceHelper.setFilterChecked(getValueDialog(2).get(position).getAlias());
				adapterLoaibuaan.notifyDataSetChanged();
				createFilter("", getValueDialog(2).get(position).getAlias(), "", "", "", "");
			}
			
		});
		
		btnXong.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.i("DATA", "JSON: "+filter);
				preferenceHelper.setFilterLoaimonan("");
				preferenceHelper.setFilterThoigian("");
				preferenceHelper.setFilterNguocgoc("");
				preferenceHelper.setFilterPhuongphap("");
				preferenceHelper.setFilterDokho("");
				preferenceHelper.setFilterSort("");
				
				
				valueLoaimon.setText("Tất cả");
				valueThoigian.setText("Tất cả");
				valueNguongoc.setText("Tất cả");
				valuePhuongphap.setText("Tất cả");
				valueDokho.setText("Tất cả");
				valueSort.setText("Tất cả");
				
				// set ve color mac dinh
				valueThoigian.setTextColor(getResources().getColor(R.color.color_text_default));
				valueLoaimon.setTextColor(getResources().getColor(R.color.color_text_default));
				valueNguongoc.setTextColor(getResources().getColor(R.color.color_text_default));
				valuePhuongphap.setTextColor(getResources().getColor(R.color.color_text_default));
				valueDokho.setTextColor(getResources().getColor(R.color.color_text_default));
				valueSort.setTextColor(getResources().getColor(R.color.color_text_default));
				
				gotoFilter(mPage, DISPLAY_LIST, filter, typeSort);
				dBolocLB.dismiss();
			}
		});
		
		
	}

	// boloc thoi gian
	public void showBolocThoigian() {
		dBolocTG = new Dialog(getActivity(), R.style.DialogSlideAnimLeft);
		dBolocTG.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dBolocTG.setContentView(R.layout.dilog_boloc_thoigian);

		// khai bao cac thanh phan
		View clickOusite = (View) dBolocTG
				.findViewById(R.id.click_outside_thoigian);
		Button btnXong = (Button) dBolocTG
				.findViewById(R.id.btn_loc_xong_thoigian);
		Button btnBack = (Button) dBolocTG
				.findViewById(R.id.btn_ve_boloc_chung_thoigian);
		ListView listThoigian = (ListView) dBolocTG
				.findViewById(R.id.list_thoigian);
		final DialogItemAdapter adapterThoigian = new DialogItemAdapter(
				getActivity(), getValueDialog(3));
		listThoigian.setAdapter(adapterThoigian);

		WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
		lp.copyFrom(dBolocTG.getWindow().getAttributes());
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;
		lp.height = WindowManager.LayoutParams.MATCH_PARENT;

		dBolocTG.setCanceledOnTouchOutside(true);
		dBolocTG.show();
		dBolocTG.getWindow().setBackgroundDrawable(new ColorDrawable(0));
		dBolocTG.getWindow().setAttributes(lp);
		dBolocTG.getWindow().setGravity(Gravity.RIGHT);

		// set su kien click cho cac thanh phan
		clickOusite.setOnClickListener(this);
		btnXong.setOnClickListener(this);
		btnBack.setOnClickListener(this);
		
		listThoigian.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				Log.i("DATA", "VALUE: alias :"+getValueDialog(3).get(position).getAlias());
				Log.i("DATA", "VALUE: name :"+getValueDialog(3).get(position).getName());
				preferenceHelper.setFilterThoigian(getValueDialog(3).get(position).getAlias());
				preferenceHelper.setFilterNameThoigian(getValueDialog(3).get(position).getName());
				preferenceHelper.setFilterChecked(getValueDialog(3).get(position).getAlias());
				adapterThoigian.notifyDataSetChanged();
				createFilter("", "" , getValueDialog(3).get(position).getAlias(), "", "", "");
			}
		});
		
		btnXong.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.i("DATA", "JSON: "+filter);
				preferenceHelper.setFilterLoaimonan("");
				preferenceHelper.setFilterLoaibuaan("");
				preferenceHelper.setFilterNguocgoc("");
				preferenceHelper.setFilterPhuongphap("");
				preferenceHelper.setFilterDokho("");
				preferenceHelper.setFilterSort("");
				
				valueLoaibuaan.setText("Tất cả");
				valueLoaimon.setText("Tất cả");
				valueNguongoc.setText("Tất cả");
				valuePhuongphap.setText("Tất cả");
				valueDokho.setText("Tất cả");
				valueSort.setText("Tất cả");
				
				// set ve color mac dinh
				valueLoaibuaan.setTextColor(getResources().getColor(R.color.color_text_default));
				valueLoaimon.setTextColor(getResources().getColor(R.color.color_text_default));
				valueNguongoc.setTextColor(getResources().getColor(R.color.color_text_default));
				valuePhuongphap.setTextColor(getResources().getColor(R.color.color_text_default));
				valueDokho.setTextColor(getResources().getColor(R.color.color_text_default));
				valueSort.setTextColor(getResources().getColor(R.color.color_text_default));
				
				gotoFilter(mPage, DISPLAY_LIST, filter, typeSort);
				dBolocTG.dismiss();
			}
		});
	}

	// boloc nguon goc
	public void showBolocNguongoc() {
		dBolocNG = new Dialog(getActivity(), R.style.DialogSlideAnimLeft);
		dBolocNG.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dBolocNG.setContentView(R.layout.dilog_boloc_nguongoc);

		// khai bao cac thanh phan
		View clickOusite = (View) dBolocNG
				.findViewById(R.id.click_outside_nguongoc);
		Button btnXong = (Button) dBolocNG
				.findViewById(R.id.btn_loc_xong_nguongoc);
		Button btnBack = (Button) dBolocNG
				.findViewById(R.id.btn_ve_boloc_chung_nguongoc);

		ListView listNguongoc = (ListView) dBolocNG
				.findViewById(R.id.list_nguongoc);
		final DialogItemAdapter adapterNguongoc = new DialogItemAdapter(
				getActivity(), getValueDialog(4));
		listNguongoc.setAdapter(adapterNguongoc);

		WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
		lp.copyFrom(dBolocNG.getWindow().getAttributes());
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;
		lp.height = WindowManager.LayoutParams.MATCH_PARENT;

		dBolocNG.setCanceledOnTouchOutside(true);
		dBolocNG.show();
		dBolocNG.getWindow().setBackgroundDrawable(new ColorDrawable(0));
		dBolocNG.getWindow().setAttributes(lp);
		dBolocNG.getWindow().setGravity(Gravity.RIGHT);

		// set su kien click cho cac thanh phan
		clickOusite.setOnClickListener(this);
		btnXong.setOnClickListener(this);
		btnBack.setOnClickListener(this);
		
		listNguongoc.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				Log.i("DATA", "VALUE: alias :"+getValueDialog(4).get(position).getAlias());
				Log.i("DATA", "VALUE: name :"+getValueDialog(4).get(position).getName());
				preferenceHelper.setFilterNguocgoc(getValueDialog(4).get(position).getAlias());
				preferenceHelper.setFilterNameNguocgoc(getValueDialog(4).get(position).getName());
				preferenceHelper.setFilterChecked(getValueDialog(4).get(position).getAlias());
				adapterNguongoc.notifyDataSetChanged();
				createFilter("", "" , "",  getValueDialog(4).get(position).getAlias(), "", "");
				
			}
		});
		
		btnXong.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.i("DATA", "JSON: "+filter);
				preferenceHelper.setFilterLoaimonan("");
				preferenceHelper.setFilterLoaibuaan("");
				preferenceHelper.setFilterThoigian("");
				preferenceHelper.setFilterPhuongphap("");
				preferenceHelper.setFilterDokho("");
				preferenceHelper.setFilterSort("");
				
				
				valueLoaibuaan.setText("Tất cả");
				valueLoaimon.setText("Tất cả");
				valueThoigian.setText("Tất cả");
				valuePhuongphap.setText("Tất cả");
				valueDokho.setText("Tất cả");
				valueSort.setText("Tất cả");
				
				// set ve color mac dinh
				valueThoigian.setTextColor(getResources().getColor(R.color.color_text_default));
				valueLoaibuaan.setTextColor(getResources().getColor(R.color.color_text_default));
				valueLoaimon.setTextColor(getResources().getColor(R.color.color_text_default));
				valuePhuongphap.setTextColor(getResources().getColor(R.color.color_text_default));
				valueDokho.setTextColor(getResources().getColor(R.color.color_text_default));
				valueSort.setTextColor(getResources().getColor(R.color.color_text_default));
				
				gotoFilter(mPage, DISPLAY_LIST, filter, typeSort);
				dBolocNG.dismiss();
			}
		});
	}

	// bo loc phuong phap
	public void showBolocPhuongphap() {
		dBolocPP = new Dialog(getActivity(), R.style.DialogSlideAnimLeft);
		dBolocPP.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dBolocPP.setContentView(R.layout.dilog_boloc_phuongphap);

		// khai bao cac thanh phan
		View clickOusite = (View) dBolocPP
				.findViewById(R.id.click_outside_phuongphap);
		Button btnXong = (Button) dBolocPP
				.findViewById(R.id.btn_loc_xong_phuongphap);
		Button btnBack = (Button) dBolocPP
				.findViewById(R.id.btn_ve_boloc_chung_phuongphap);

		ListView listPhuongphap = (ListView) dBolocPP
				.findViewById(R.id.list_phuongphap);
		final DialogItemAdapter adapterPhuongphap = new DialogItemAdapter(
				getActivity(), getValueDialog(5));
		listPhuongphap.setAdapter(adapterPhuongphap);

		WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
		lp.copyFrom(dBolocPP.getWindow().getAttributes());
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;
		lp.height = WindowManager.LayoutParams.MATCH_PARENT;

		dBolocPP.setCanceledOnTouchOutside(true);
		dBolocPP.show();
		dBolocPP.getWindow().setBackgroundDrawable(new ColorDrawable(0));
		dBolocPP.getWindow().setAttributes(lp);
		dBolocPP.getWindow().setGravity(Gravity.RIGHT);

		// set su kien click cho cac thanh phan
		clickOusite.setOnClickListener(this);
		btnXong.setOnClickListener(this);
		btnBack.setOnClickListener(this);
		
		listPhuongphap.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				Log.i("DATA", "VALUE: alias :"+getValueDialog(5).get(position).getAlias());
				Log.i("DATA", "VALUE: name :"+getValueDialog(5).get(position).getName());
				preferenceHelper.setFilterPhuongphap(getValueDialog(5).get(position).getAlias());
				preferenceHelper.setFilterNamePhuongphap(getValueDialog(5).get(position).getName());
				preferenceHelper.setFilterChecked(getValueDialog(5).get(position).getAlias());
				adapterPhuongphap.notifyDataSetChanged();
				createFilter("", "" , "",  "", getValueDialog(5).get(position).getAlias(), "");
			}
			
		});
		
		btnXong.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.i("DATA", "JSON: "+filter);
				preferenceHelper.setFilterLoaimonan("");
				preferenceHelper.setFilterLoaibuaan("");
				preferenceHelper.setFilterThoigian("");
				preferenceHelper.setFilterNguocgoc("");
				preferenceHelper.setFilterDokho("");
				preferenceHelper.setFilterSort("");
				
				
				valueLoaibuaan.setText("Tất cả");
				valueLoaimon.setText("Tất cả");
				valueThoigian.setText("Tất cả");
				valueNguongoc.setText("Tất cả");
				valueDokho.setText("Tất cả");
				valueSort.setText("Tất cả");
				
				// set ve color mac dinh
				valueThoigian.setTextColor(getResources().getColor(R.color.color_text_default));
				valueLoaibuaan.setTextColor(getResources().getColor(R.color.color_text_default));
				valueLoaimon.setTextColor(getResources().getColor(R.color.color_text_default));
				valueNguongoc.setTextColor(getResources().getColor(R.color.color_text_default));
				valueDokho.setTextColor(getResources().getColor(R.color.color_text_default));
				valueSort.setTextColor(getResources().getColor(R.color.color_text_default));
				
				gotoFilter(mPage, DISPLAY_LIST, filter, typeSort);
				dBolocPP.dismiss();
				
			}
		});
		
		
	}

	// bo loc do kho
	// bo loc phuong phap
	public void showBolocDokho() {
		dBolocDK = new Dialog(getActivity(), R.style.DialogSlideAnimLeft);
		dBolocDK.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dBolocDK.setContentView(R.layout.dilog_boloc_dokho);

		// khai bao cac thanh phan
		View clickOusite = (View) dBolocDK
				.findViewById(R.id.click_outside_dokho);
		Button btnXong = (Button) dBolocDK
				.findViewById(R.id.btn_loc_xong_dokho);
		Button btnBack = (Button) dBolocDK
				.findViewById(R.id.btn_ve_boloc_chung_dokho);

		ListView listDokho = (ListView) dBolocDK
				.findViewById(R.id.list_dokho);
		final DialogItemAdapter adapterDokho = new DialogItemAdapter(
				getActivity(), getValueDialog(6));
		listDokho.setAdapter(adapterDokho);

		WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
		lp.copyFrom(dBolocDK.getWindow().getAttributes());
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;
		lp.height = WindowManager.LayoutParams.MATCH_PARENT;

		dBolocDK.setCanceledOnTouchOutside(true);
		dBolocDK.show();
		dBolocDK.getWindow().setBackgroundDrawable(new ColorDrawable(0));
		dBolocDK.getWindow().setAttributes(lp);
		dBolocDK.getWindow().setGravity(Gravity.RIGHT);

		// set su kien click cho cac thanh phan
		clickOusite.setOnClickListener(this);
		btnXong.setOnClickListener(this);
		btnBack.setOnClickListener(this);
		
		listDokho.setOnItemClickListener(new OnItemClickListener() {
			

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				Log.i("DATA", "VALUE: alias :"+getValueDialog(6).get(position).getAlias());
				Log.i("DATA", "VALUE: name :"+getValueDialog(6).get(position).getName());
				preferenceHelper.setFilterDokho(getValueDialog(6).get(position).getAlias());
				preferenceHelper.setFilterNameDokho(getValueDialog(6).get(position).getName());
				preferenceHelper.setFilterChecked(getValueDialog(6).get(position).getAlias());
				adapterDokho.notifyDataSetChanged();
				createFilter("", "" , "", "" , "", getValueDialog(6).get(position).getAlias());
				
			}
		});
		
		btnXong.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.i("DATA", "JSON: "+filter);
				preferenceHelper.setFilterLoaimonan("");
				preferenceHelper.setFilterLoaibuaan("");
				preferenceHelper.setFilterThoigian("");
				preferenceHelper.setFilterNguocgoc("");
				preferenceHelper.setFilterPhuongphap("");
				preferenceHelper.setFilterSort("");
				
				
				valueLoaibuaan.setText("Tất cả");
				valueLoaimon.setText("Tất cả");
				valueThoigian.setText("Tất cả");
				valueNguongoc.setText("Tất cả");
				valuePhuongphap.setText("Tất cả");
				valueSort.setText("Tất cả");
				
				// set ve color mac dinh
				valueThoigian.setTextColor(getResources().getColor(R.color.color_text_default));
				valueLoaibuaan.setTextColor(getResources().getColor(R.color.color_text_default));
				valueLoaimon.setTextColor(getResources().getColor(R.color.color_text_default));
				valueNguongoc.setTextColor(getResources().getColor(R.color.color_text_default));
				valuePhuongphap.setTextColor(getResources().getColor(R.color.color_text_default));
				valueDokho.setTextColor(getResources().getColor(R.color.color_text_default));
				valueSort.setTextColor(getResources().getColor(R.color.color_text_default));
				
				gotoFilter(mPage, DISPLAY_LIST, filter, typeSort);
				dBolocDK.dismiss();
			}
		});
		
	}

	// trueyen du lieu khi click vao cac thanh phan nguyen lieu
	public ArrayList<ObjAttribute> getValueDialog(int type) {
		if (type == 1) {
			return listLoaimonan;
		}
		if (type == 2) {
			return listLoaibuaan;
		}
		if (type == 3) {
			
			return listThoigian;
		}
		if (type == 4) {
			return listNguongoc;
		}
		if (type == 5) {
			return listPhuongphap;
		}
		if (type == 6) {
			return listDokho;
		}
		
		if (type == 0) {
			
			return listSort;
		}
		return null;
	}
	
	public ArrayList<ObjAttribute> creatSort(){
		listSort = new ArrayList<ObjAttribute>();
		listSort.add(new ObjAttribute("view_count", "Xem nhiều nhất"));
		listSort.add(new ObjAttribute("created", "Mới nhất"));
		listSort.add(new ObjAttribute("like_count", "Thích nhiều nhất"));
		listSort.add(new ObjAttribute("rating_avg", "Đánh giá tốt nhất"));
		return listSort;
	}
	
	// trueyen du lieu khi click vao cac thanh phan nguyen lieu
		public String aliasDialog(int type) {
			if (type == 1) {
				alias = "loai-mon-an";
			}
			if (type == 2) {
				alias = "loai-bua-an";
			}
			if (type == 3) {
				
				alias = "thoi-gian";
			}
			if (type == 4) {
				alias = "nguon-goc";
			}
			if (type == 5) {
				alias = "phuong-phap";
			}
			if (type == 6) {
				alias = "do-kho";
			}
			
			return null;
		}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		int l = visibleItemCount + firstVisibleItem;
		if (l == totalItemCount && canLoadMore == true && isLoading == false) {
			// get more data
		} else {

		}

	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {

	}

	void addFooterView() {
		View loadMoreView = ((LayoutInflater) this.getActivity()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.footer_loadmore, null, false);
		footerMain = (View) loadMoreView.findViewById(R.id.footer_main);
		footerMain.setVisibility(View.GONE);
		getListView().addFooterView(loadMoreView);
		btnLoadMore = (Button) loadMoreView.findViewById(R.id.btn_load_more);
		progressBarLoadmore = (ProgressBar) loadMoreView
				.findViewById(R.id.progressBar1);
		btnLoadMore.setOnClickListener(this);
	}

	// hide progress bar
	void hiddenProgessDialog() {
		try {
			getActivity().runOnUiThread(new Runnable() {

				@Override
				public void run() {
					try {
						progressBar.setVisibility(View.GONE);
					} catch (Exception e) {
					}
				}
			});
		} catch (Exception e) {
		}
	}

	void hiddenProgessLoadmore() {
		try {
			getActivity().runOnUiThread(new Runnable() {

				@Override
				public void run() {
					try {
						progressBar.setVisibility(View.GONE);
					} catch (Exception e) {
					}
				}
			});
		} catch (Exception e) {
		}
	}

	public void searchCT(final int page, final int typeDisplay) {
		isLoading = true;
		isStateSearch = true;
		isStateFilter = false;

		// hien lop mo khi searh va tro ve trang thai ban dau
		progressBar.setVisibility(View.VISIBLE);
		llFocus.setVisibility(View.GONE);
		btnHuySearch.setVisibility(View.GONE);
		btnSort.setVisibility(View.VISIBLE);
		btnSortDisplay.setVisibility(View.VISIBLE);
		KeyboardHelper.hideSoftKeyboard(getActivity());
		llFocus.setFocusableInTouchMode(true);

		HttpClientHelper httpClientHelper = new HttpClientHelper(
				new MyJsonHttpResponseHandler() {

					@Override
					public void onFailure(Throwable error) {
						progressBar.setVisibility(View.GONE);
						progressBarLoadmore.setVisibility(View.GONE);
						btnLoadMore.setText("Lấy thêm công thức");
						isLoading = false;
						progressBar1.setVisibility(View.GONE);
						hiddenProgessDialog();
						error.printStackTrace();
					}

					@Override
					public void onSuccess(JSONObject re) {
						hiddenProgessDialog();
						try {
							int status = re.getInt("status");
							if (status == 200) {
								
								preferenceHelper.setFilterDokho("");
								preferenceHelper.setFilterLoaibuaan("");
								preferenceHelper.setFilterLoaimonan("");
								preferenceHelper.setFilterNguocgoc("");
								preferenceHelper.setFilterPhuongphap("");
								preferenceHelper.setFilterThoigian("");
								preferenceHelper.setFilterSort("");
								
								setDefaultBacgroundButtonBoloc();
								
								progressBar.setVisibility(View.GONE);
								progressBarLoadmore.setVisibility(View.GONE);
								btnLoadMore.setText("Lấy thêm công thức");
								progressBar1.setVisibility(View.GONE);
								JSONObject data = re.getJSONObject("data");
								JSONArray items = data.getJSONArray("items");
								if (page == 0) {
									adapterList.clear();
									adapterGrid.clear();
								}
								countRs = data.getInt("total");

								for (int i = 0; i < items.length(); i++) {
									JSONObject item = items.getJSONObject(i);

									String id = item.getString("id");
									String name = item.getString("name");
									String desc = item.getString("desc");
									String time = item.getString("time");
									// getImage
									JSONObject imageObj = item.getJSONObject("image");
									String image = imageObj.getString("cover");
									
									int viewCount = item.getInt("view_count");
									int likeCount = item.getInt("like_count");

									ObjCookpad objCookpad = new ObjCookpad();
									Object tacgia = item.get("author");
									if (tacgia instanceof JSONObject) {
										JSONObject jsonObj = (JSONObject) tacgia;
										objCookpad.setNamePersonCookpad(jsonObj
												.getString("name"));
										objCookpad.setIdNamePerson(jsonObj.getString("id"));
									} else if (tacgia instanceof JSONArray) {
										JSONArray jsonArray = (JSONArray) tacgia;
										objCookpad
												.setNamePersonCookpad("Vô danh");
										objCookpad.setIdNamePerson("");
									}

									objCookpad.setId(id);
									objCookpad.setNameCookpad(name);
									objCookpad.setDescriptionCookpad(desc);
									objCookpad.setUrlImageCookpad(image);
									objCookpad.setViewedCookpad(viewCount);
									objCookpad.setLikeCookpad(likeCount);
									objCookpad.setThoigianNauan(time);

									objCookpads.add(objCookpad);

								}
								isLoading = false;
								if (typeDisplay == DISPLAY_LIST) {
//									adapterList.notifyDataSetChanged();
								} else {
									adapterGrid.notifyDataSetChanged();
								}
								
								if (items.length() < 10){
									footerMain.setVisibility(View.GONE);
								} else {
									footerMain.setVisibility(View.VISIBLE);
								}
								
								if (items.length() == 0) {
									footerMain.setVisibility(View.GONE);
									txt_no_result.setVisibility(View.VISIBLE);
									edtSearch.setText("");
									return;
								}

								// hien thi layout ket qua
								llSearchRs.setVisibility(View.VISIBLE);
								keyWordSearch.setText("Từ khóa: " + keyword
										+ " " + "( " + countRs + " kết quả)");

							}
						} catch (JSONException e) {
							progressBarLoadmore.setVisibility(View.GONE);
							btnLoadMore.setText("Lấy thêm công thức");
							isLoading = false;
							progressBar1.setVisibility(View.GONE);
							hiddenProgessDialog();
							e.printStackTrace();
						}

					}

				});
		JSONObject filters = new JSONObject();
		httpClientHelper.searchMonan(edtSearch.getText().toString(), filters,
				page, "like_count");
		
		if (page==0) {
			getListView().setSelection(0);
		}
	}
	
	void getAttribute() {
		HttpClientHelper httpClientHelper = new HttpClientHelper(new MyJsonHttpResponseHandler(){

			@Override
			public void onFailure(Throwable error) {
				super.onFailure(error);
			}

			@Override
			public void onSuccess(JSONObject re) {
				try {
					if(!re.toString().equals("")) {
						preferenceHelper.setDataFilter(re.toString());
					}
				} catch (Exception e) { }
				parseData(re);
			}
			
		});
		httpClientHelper.getThuoctinh();
	}
	
	public void parseData(JSONObject re) {
		try {
			int status = re.getInt("status");
			if (status == 200) {
				listDokho.clear();
				listLoaibuaan.clear();
				listLoaimonan.clear();
				listNguongoc.clear();
				listPhuongphap.clear();
				listThoigian.clear();
				
				JSONObject data = re.getJSONObject("data");
				JSONArray params = data.getJSONArray("param");
				Log.i("DATA", "ATRIBUTE: "+params);
				
				// get loai mon an
				JSONObject loaimon = params.getJSONObject(0);
				JSONArray loaimonParams = loaimon.getJSONArray("params");
				for (int i = 0; i < loaimonParams.length(); i++) {
					ObjAttribute objAttribute = new ObjAttribute();
					JSONObject loaimonParam = loaimonParams.getJSONObject(i);
					String alias = loaimonParam.getString("alias");
					String name = loaimonParam.getString("name");
					objAttribute.setAlias(alias);
					objAttribute.setName(name);
					listLoaimonan.add(objAttribute);
				}
				
				// get loai bua an
				JSONObject loaibuaan = params.getJSONObject(1);
				JSONArray loaibuaanParams = loaibuaan.getJSONArray("params");
				for (int i = 0; i < loaibuaanParams.length(); i++) {
					ObjAttribute objAttribute = new ObjAttribute();
					JSONObject loaibuaanParam = loaibuaanParams.getJSONObject(i);
					String alias = loaibuaanParam.getString("alias");
					String name = loaibuaanParam.getString("name");
					objAttribute.setAlias(alias);
					objAttribute.setName(name);
					listLoaibuaan.add(objAttribute);
				}
				
				// get thoi gian
				JSONObject thoigian = params.getJSONObject(2);
				JSONArray thoigianParams = thoigian.getJSONArray("params");
				for (int i = 0; i < thoigianParams.length(); i++) {
					ObjAttribute objAttribute = new ObjAttribute();
					JSONObject thoigianParam = thoigianParams.getJSONObject(i);
					String alias = thoigianParam.getString("alias");
					String name = thoigianParam.getString("name");
					objAttribute.setAlias(alias);
					objAttribute.setName(name);
					listThoigian.add(objAttribute);
				}
				
				// get nguon goc
				JSONObject nguongoc = params.getJSONObject(3);
				JSONArray nguongocParams = nguongoc.getJSONArray("params");
				for (int i = 0; i < nguongocParams.length(); i++) {
					ObjAttribute objAttribute = new ObjAttribute();
					JSONObject nguongocParam = nguongocParams.getJSONObject(i);
					String alias = nguongocParam.getString("alias");
					String name = nguongocParam.getString("name");
					objAttribute.setAlias(alias);
					objAttribute.setName(name);
					listNguongoc.add(objAttribute);
				}
				
				// get phuong phap
				JSONObject phuongphap = params.getJSONObject(4);
				JSONArray phuongphapParams = phuongphap.getJSONArray("params");
				for (int i = 0; i < phuongphapParams.length(); i++) {
					ObjAttribute objAttribute = new ObjAttribute();
					JSONObject phuongphapParam = phuongphapParams.getJSONObject(i);
					String alias = phuongphapParam.getString("alias");
					String name = phuongphapParam.getString("name");
					objAttribute.setAlias(alias);
					objAttribute.setName(name);
					listPhuongphap.add(objAttribute);
				}
				
				// get do kho
				JSONObject dokho = params.getJSONObject(5);
				JSONArray dokhoParams = dokho.getJSONArray("params");
				for (int i = 0; i < dokhoParams.length(); i++) {
					ObjAttribute objAttribute = new ObjAttribute();
					JSONObject dokhoParam = dokhoParams.getJSONObject(i);
					String alias = dokhoParam.getString("alias");
					String name = dokhoParam.getString("name");
					objAttribute.setAlias(alias);
					objAttribute.setName(name);
					listDokho.add(objAttribute);
				}
				
			} else {
				Toast.makeText(getActivity(), "Không lấy được dữ liệu",  Toast.LENGTH_SHORT).show();
			}
		} catch (JSONException e) {
			e.printStackTrace();
			Toast.makeText(getActivity(), "Không lấy được dữ liệu",  Toast.LENGTH_SHORT).show();
		}
	}
	
	void loadCache() {
		String cache = preferenceHelper.getDataFilter();
		if(!cache.equals("")) {
			try {
				JSONObject response = new JSONObject(cache);
				parseData(response);
			} catch (Exception e) { }
		}
	}
	
	
	void gotoFilter(final int page, final int typeDisplay, JSONObject filter, String typeSort) {
		isStateFilter = true;
		isStateSearch = false;
		isUsed = true;
		HttpClientHelper httpClientHelper = new HttpClientHelper(
				new MyJsonHttpResponseHandler() {

					@Override
					public void onFailure(Throwable error) {
						progressBar.setVisibility(View.GONE);
						progressBarLoadmore.setVisibility(View.GONE);
						btnLoadMore.setText("Lấy thêm công thức");
						progressBar1.setVisibility(View.GONE);
						isLoading = false;
						hiddenProgessDialog();
						error.printStackTrace();
					}

					@Override
					public void onSuccess(JSONObject re) {
						hiddenProgessDialog();
						try {
							int status = re.getInt("status");
							if (status == 200) {
								
								edtSearch.setText("");
								keyword = "";
								llSearchRs.setVisibility(View.GONE);
								
								progressBar.setVisibility(View.GONE);
								progressBarLoadmore.setVisibility(View.GONE);
								btnLoadMore.setText("Lấy thêm công thức");
								progressBar1.setVisibility(View.GONE);
								
								JSONObject data = re.getJSONObject("data");
								int total = data.getInt("total");
								JSONArray items = data.getJSONArray("items");
								
								if (items.length() == 0) {
									footerMain.setVisibility(View.GONE);
									progressBarLoadmore.setVisibility(View.GONE);
									txt_no_result.setVisibility(View.VISIBLE);
									return;
								}
								txt_no_result.setVisibility(View.GONE);
								if((items.length() < 10) && (items.length()>0)) {
									footerMain.setVisibility(View.GONE);
									progressBarLoadmore.setVisibility(View.GONE);
								}
								
								if (page == 0) {
									objCookpads.clear();
								}
								countRs = data.getInt("total");

								for (int i = 0; i < items.length(); i++) {
									JSONObject item = items.getJSONObject(i);

									String id = item.getString("id");
									String name = item.getString("name");
									String desc = item.getString("desc");
									// getImage
									JSONObject imageObj = item.getJSONObject("image");
									String image = imageObj.getString("cover");
									
									int viewCount = item.getInt("view_count");
									int likeCount = item.getInt("like_count");

									ObjCookpad objCookpad = new ObjCookpad();
									Object tacgia = item.get("author");
									if (tacgia instanceof JSONObject) {
										JSONObject jsonObj = (JSONObject) tacgia;
										objCookpad.setNamePersonCookpad(jsonObj
												.getString("name"));
										objCookpad.setIdNamePerson(jsonObj.getString("id"));
									} else if (tacgia instanceof JSONArray) {
										JSONArray jsonArray = (JSONArray) tacgia;
										objCookpad
												.setNamePersonCookpad("Vô danh");
										objCookpad.setIdNamePerson("");
									}
									objCookpad.setThoigianNauan(item
											.getString("time"));
									objCookpad.setId(id);
									objCookpad.setNameCookpad(name);
									objCookpad.setDescriptionCookpad(desc);
									objCookpad.setUrlImageCookpad(image);
									objCookpad.setViewedCookpad(viewCount);
									objCookpad.setLikeCookpad(likeCount);

									objCookpads.add(objCookpad);

								}
								isLoading = false;
								if (page == 0) {
									adapterList.notifyDataSetChanged();
								}
//								adapterGrid.notifyDataSetChanged();
								footerMain.setVisibility(View.VISIBLE);

								setDefaultBacgroundButtonBoloc();
							}
						} catch (JSONException e) {
							progressBarLoadmore.setVisibility(View.GONE);
							btnLoadMore.setText("Lấy thêm công thức");
							isLoading = false;
							progressBar1.setVisibility(View.GONE);
							hiddenProgessDialog();
							e.printStackTrace();
						}

					}

				});
		httpClientHelper.searchMonan("", filter, page, typeSort);
	}
	
}
